﻿namespace ADDataAccess.Tests {

    #region Usings
    using Contoso.ActiveDirectory;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.DirectoryServices;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Security.Principal;
    using System.Text;
    using System.Threading.Tasks; 
    #endregion

    [TestClass]
    public class HelperMethodTests {

        [TestMethod]
        public void Test_GetAdjustedAuthenticationType() {

            var authenticationType = AuthenticationTypes.None;
            var bindPath = "LDAP://contosomddc1.contos.com/DC=contoso,DC=com";
            authenticationType = ADDataAccess.GetAdjustedAuthenticationType(bindPath);
            Assert.IsTrue(authenticationType.HasFlag(AuthenticationTypes.ServerBind));

            authenticationType = AuthenticationTypes.None;
            bindPath = "LDAP://contosomddc1.contos.com/OU=HQ,DC=contoso,DC=com";
            authenticationType = ADDataAccess.GetAdjustedAuthenticationType(bindPath);
            Assert.IsTrue(authenticationType.HasFlag(AuthenticationTypes.ServerBind));

            authenticationType = AuthenticationTypes.None;
            bindPath = "LDAP://contosomddc1.contos.com/CN=Test001,OU=HQ,DC=contoso,DC=com";
            authenticationType = ADDataAccess.GetAdjustedAuthenticationType(bindPath);
            Assert.IsTrue(authenticationType.HasFlag(AuthenticationTypes.ServerBind));

            authenticationType = AuthenticationTypes.Secure | AuthenticationTypes.Sealing | AuthenticationTypes.Signing | AuthenticationTypes.ServerBind;
            bindPath = "LDAP://DC=contoso,DC=com";
            authenticationType = ADDataAccess.GetAdjustedAuthenticationType(bindPath, authenticationType);
            Assert.IsFalse(authenticationType.HasFlag(AuthenticationTypes.ServerBind));

            authenticationType = AuthenticationTypes.Secure | AuthenticationTypes.Sealing | AuthenticationTypes.Signing | AuthenticationTypes.ServerBind;
            bindPath = "LDAP://OU=HQ,DC=contoso,DC=com";
            authenticationType = ADDataAccess.GetAdjustedAuthenticationType(bindPath, authenticationType);
            Assert.IsFalse(authenticationType.HasFlag(AuthenticationTypes.ServerBind));

            authenticationType = AuthenticationTypes.Secure | AuthenticationTypes.Sealing | AuthenticationTypes.Signing | AuthenticationTypes.ServerBind;
            bindPath = "LDAP://CN=Test001,OU=HQ,DC=contoso,DC=com";
            authenticationType = ADDataAccess.GetAdjustedAuthenticationType(bindPath, authenticationType);
            Assert.IsFalse(authenticationType.HasFlag(AuthenticationTypes.ServerBind));

            authenticationType = AuthenticationTypes.Secure | AuthenticationTypes.Sealing | AuthenticationTypes.Signing | AuthenticationTypes.ServerBind;
            bindPath = "LDAP://RootDSE";
            authenticationType = ADDataAccess.GetAdjustedAuthenticationType(bindPath, authenticationType);
            Assert.IsFalse(authenticationType.HasFlag(AuthenticationTypes.ServerBind));
        }

        [TestMethod]
        public void Test_GetCNFromDN() {
            string cn = "jsmith001";
            string dn = string.Format("CN={0},OU=Users,OU=TSTOU,DC=contoso,DC=com", cn);
            string cnFromDN = ADDataAccess.GetCNFromDN(dn);
            Assert.AreEqual(cnFromDN, cn);

            dn = string.Format("CN={0},CN=Users,DC=contoso,DC=com", cn);
            cnFromDN = ADDataAccess.GetCNFromDN(dn);
            Assert.AreEqual(cnFromDN, cn);

            dn = string.Format("CN={0},DC=contoso,DC=com", cn);
            cnFromDN = ADDataAccess.GetCNFromDN(dn);
            Assert.AreEqual(cnFromDN, cn);
        }

        [TestMethod]
        public void Test_GetDomainDNFromDN() {

            string dn = "CN=jadoe123,ou=users,ou=hq,dc=contoso,dc=com";
            var domainDN = ADDataAccess.GetDomainDNFromDN(dn);

            Assert.IsNotNull(domainDN);
            Assert.AreEqual(domainDN, "dc=contoso,dc=com");
        }

        [TestMethod]
        public void Test_GetDomainDnsName() {
            string dn = "DC=c.root-servers.net,DC=RootDNSServers,CN=MicrosoftDNS,CN=System,DC=contoso,DC=com";
            string domainDnsName = ADDataAccess.GetDomainDnsName(dn);

            Assert.AreEqual(domainDnsName, "contoso.com");

            dn = "CN=System,DC=contoso,DC=com";
            domainDnsName = ADDataAccess.GetDomainDnsName(dn);
            Assert.AreEqual(domainDnsName, "contoso.com");

            dn = "DC=contoso,DC=com";
            domainDnsName = ADDataAccess.GetDomainDnsName(dn);
            Assert.AreEqual(domainDnsName, "contoso.com");

            dn = "OU=Users,OU=TSTOU,DC=contoso,DC=com";
            domainDnsName = ADDataAccess.GetDomainDnsName(dn);
            Assert.AreEqual(domainDnsName, "contoso.com");
        }

        [TestMethod]
        public void Test_GetDomainDnsNameFromCanonicalName() {
            string canonicalName = "contoso.com/TSTOU/Users/jsmith001";
            string domainDnsName = ADDataAccess.GetDomainDnsNameFromCanonicalName(canonicalName);

            Assert.AreEqual(domainDnsName, "contoso.com");
        }

        [TestMethod]
        public void Test_GetNameFromCanonicalName() {
            string canonicalName = "contoso.com/TSTOU/Users/jsmith001";
            string name = ADDataAccess.GetNameFromCanonicalName(canonicalName);

            Assert.AreEqual(name, "jsmith001");
        }

        [TestMethod]
        public void Test_GetGetObjectClassObjectCategoryForSearchFilter() {

            string filter = ADDataAccess.GetObjectClassObjectCategoryForSearchFilter<UserAccount>(null);
            Assert.AreEqual(string.Format("(&(objectCategory={0})(objectClass={1}))", 
                UserAccount.ObjectCategoryShortName, UserAccount.ObjectClassShortName), filter);

            filter = ADDataAccess.GetObjectClassObjectCategoryForSearchFilter<UserAccount>("(objectCategory=person)");
            Assert.AreEqual(string.Format("(&(objectClass={0})(objectCategory={1}))", 
                UserAccount.ObjectClassShortName, UserAccount.ObjectCategoryShortName), filter);

            filter = ADDataAccess.GetObjectClassObjectCategoryForSearchFilter<UserAccount>("(samAccountName=jdoe001)");
            Assert.AreEqual(string.Format("(&(objectCategory={1})(objectClass={0})(samAccountName=jdoe001))",
                UserAccount.ObjectClassShortName, UserAccount.ObjectCategoryShortName), filter);

            filter = ADDataAccess.GetObjectClassObjectCategoryForSearchFilter<UserAccount>("(isDeleted=TRUE)");
            Assert.AreEqual(string.Format("(&(objectClass={0})(isDeleted=TRUE))", UserAccount.ObjectClassShortName), filter);

            filter = ADDataAccess.GetObjectClassObjectCategoryForSearchFilter<ADBaseObjectProperties>(null);
            Assert.IsNull(filter);

            filter = ADDataAccess.GetObjectClassObjectCategoryForSearchFilter<DomainProperties>(null);
            Assert.AreEqual(string.Format("(objectCategory={0})", DomainProperties.ObjectCategoryShortName), filter);
        }

        [TestMethod]
        public void Test_GetGpLinkGPODNs() {
            string gpLink = "[LDAP://cn={E5ED3685-8822-4C9B-86E4-49D98A5AA608},cn=policies,cn=system,DC=contoso,DC=com;0][LDAP://CN={6AC1786C-016F-11D2-945F-00C04fB984F9},CN=Policies,CN=System,DC=contoso,DC=com;0]";
            var gpoDNs = ADDataAccess.GetGPLinkGPODNs(gpLink);

            Assert.IsNotNull(gpoDNs);
            Assert.IsTrue(gpoDNs.Count == 2);
            Assert.AreEqual(gpoDNs[0], "cn={E5ED3685-8822-4C9B-86E4-49D98A5AA608},cn=policies,cn=system,DC=contoso,DC=com");
            Assert.AreEqual(gpoDNs[1], "CN={6AC1786C-016F-11D2-945F-00C04fB984F9},CN=Policies,CN=System,DC=contoso,DC=com");
        }

        [TestMethod]
        public void Test_GetParentDN() {
            var dn = "CN=2014-02-04T12:30:21-05:00{B34406B4-15A9-428C-BB9B-DC8BA6D5F41B},CN=CONTOSOMDDC1,OU=Domain Controllers,DC=contoso,DC=com";
            var parentDN = ADDataAccess.GetParentDN(dn);
            Assert.AreEqual(parentDN, "CN=CONTOSOMDDC1,OU=Domain Controllers,DC=contoso,DC=com");

            dn = "CN=jsmith001,OU=Users,OU=HQ,DC=contoso,DC=com";
            parentDN = ADDataAccess.GetParentDN(dn);
            Assert.AreEqual(parentDN, "OU=Users,OU=HQ,DC=contoso,DC=com");

            dn = "CN=Administrator,CN=Users,DC=contoso,DC=com";
            parentDN = ADDataAccess.GetParentDN(dn);
            Assert.AreEqual(parentDN, "CN=Users,DC=contoso,DC=com");

            dn = "DC=RootDNSServers,CN=MicrosoftDNS,CN=System,DC=contoso,DC=com";
            parentDN = ADDataAccess.GetParentDN(dn);
            Assert.AreEqual(parentDN, "CN=MicrosoftDNS,CN=System,DC=contoso,DC=com");
        }

        [TestMethod]
        public void Test_SpacesCharacterHandling() {
            Debug.WriteLine("{0}", DateTime.Now.ToString());
            string unEncodedDN = @"CN=Group Name \ ,OU=Exchange Groups,DC=contoso,DC=com";
            string expectedEncodedDN = @"CN=Group\20Name\20\ ,OU=Exchange\20Groups,DC=contoso,DC=com";
            string encodedDN = ADDataAccess.DNEncode(unEncodedDN);

            Assert.AreEqual(encodedDN, expectedEncodedDN);
        }

        [TestMethod]
        public void Test_SpecialCharacters() {
            Debug.WriteLine("{0}", DateTime.Now.ToString());
            string unEncodedDN = @"CN=GRP-ADTESTCHARACTERS#+<>;" + "\"" + @"/=,\&!|-'@%20,OU=Groups,DC=contoso,DC=com";
            string expectedEncodedDN = @"CN=GRP-ADTESTCHARACTERS\23\2B\3C\3E\3B\22\2F\3D\2C\5C&!|-'@%20,OU=Groups,DC=contoso,DC=com";
            string encodedDN = ADDataAccess.DNEncode(unEncodedDN);

            Assert.AreEqual(encodedDN, expectedEncodedDN);
        }

        [TestMethod]
        public void Test_UACConversion() {
            var uac = UserAccountControlFlags.NormalAccount | UserAccountControlFlags.PasswordNeverExpires;
            var newUac = uac;
            newUac &= ~UserAccountControlFlags.PasswordNeverExpires;

            Assert.IsFalse(newUac.HasFlag(UserAccountControlFlags.PasswordNeverExpires));

            newUac |= UserAccountControlFlags.PasswordNeverExpires;
            Assert.IsTrue(newUac.HasFlag(UserAccountControlFlags.PasswordNeverExpires));

            uac = (UserAccountControlFlags)66049;
            Debug.WriteLine(uac.ToString());
            Assert.AreEqual(uac.ToString(), "Script, NormalAccount, PasswordNeverExpires");

            uac = (UserAccountControlFlags)8389185;
            Debug.WriteLine(uac.ToString());
            Assert.AreEqual(uac.ToString(), "Script, CannotChangePassword, NormalAccount, PasswordExpired");

            uac = (UserAccountControlFlags)8389121;
            Debug.WriteLine(uac.ToString());
            Assert.AreEqual(uac.ToString(), "Script, NormalAccount, PasswordExpired");

            uac = (UserAccountControlFlags)9;
            Debug.WriteLine(uac.ToString());
            Debug.WriteLine(Enum.IsDefined(typeof(UserAccountControlFlags),uac));

            uac = (UserAccountControlFlags)3;
            Debug.WriteLine(uac.ToString());
            Debug.WriteLine(Enum.IsDefined(typeof(UserAccountControlFlags), uac));
        }

        [TestMethod]
        public void Test_RemoteDesktopAttributes_GenerateUserParameters_DefaultValues() {

            var userParametersDefaultValues = File.ReadAllText(@"..\..\..\Collateral\userParameters-DefaultValues.txt");
            var remoteDesktopAttributes= new RemoteDesktopAttributes();
            var userParametersGenerated = remoteDesktopAttributes.GenerateUserParameters();
            Assert.AreEqual(userParametersDefaultValues, userParametersGenerated);
        }

        [TestMethod]
        public void Test_RemoteDesktopAttributes_GenerateUserParameters_LogonDisabledFlag() {

            var ctxCfgFlags1 =
                CtxCfgFlags1.F1MSK_DISABLEENCRYPTION
                | CtxCfgFlags1.F1MSK_FORCECLIENTLPTDEF
                | CtxCfgFlags1.F1MSK_AUTOCLIENTLPTS
                | CtxCfgFlags1.F1MSK_AUTOCLIENTDRIVES
                | CtxCfgFlags1.F1MSK_INHERITAUTOCLIENT
                | CtxCfgFlags1.F1MSK_INHERITINITIALPROGRAM
                | CtxCfgFlags1.Unknown80000000;

            Assert.IsFalse(ctxCfgFlags1.HasFlag(CtxCfgFlags1.F1MSK_LOGONDISABLED));
            ctxCfgFlags1 |= CtxCfgFlags1.F1MSK_LOGONDISABLED;

            Assert.IsTrue(ctxCfgFlags1.HasFlag(CtxCfgFlags1.F1MSK_LOGONDISABLED));

            ctxCfgFlags1 &= ~CtxCfgFlags1.F1MSK_LOGONDISABLED;
            Assert.IsFalse(ctxCfgFlags1.HasFlag(CtxCfgFlags1.F1MSK_LOGONDISABLED));
        }

        [TestMethod]
        public void Test_RemoteDesktopAttributes_ProcessResults() {
            string userParameters = File.ReadAllText(@"..\..\..\Collateral\userParameters1.txt");
            var attributes = new Dictionary<string, object[]>(StringComparer.OrdinalIgnoreCase);
            attributes.Add("userParameters", new string[] { userParameters });
            var remoteDesktopAttributes = new RemoteDesktopAttributes();
            remoteDesktopAttributes.ProcessResults(attributes);

            Assert.IsTrue(remoteDesktopAttributes.IsValid);
            Assert.IsFalse(remoteDesktopAttributes.AllowLogon);
            Assert.IsTrue(remoteDesktopAttributes.ConnectClientDrivesAtLogon);
            Assert.IsTrue(remoteDesktopAttributes.ConnectClientPrintersAtLogon);
            Assert.IsTrue(remoteDesktopAttributes.DefaultToMainPrinter);
            Assert.AreEqual(remoteDesktopAttributes.DisconnectAction, RemoteDesktopDisconnectAction.Logoff);
            Assert.AreEqual(remoteDesktopAttributes.MaxConnectionTime, TimeSpan.FromDays(2));
            Assert.AreEqual(remoteDesktopAttributes.MaxDisconnectionTime, TimeSpan.FromHours(3));
            Assert.AreEqual(remoteDesktopAttributes.MaxIdleTime, TimeSpan.FromDays(1));
            Assert.AreEqual(remoteDesktopAttributes.ReconnectAction, RemoteDesktopReconnectAction.FromOriginatingClientOnly);
            Assert.IsTrue(remoteDesktopAttributes.RemoteControlEnabled);
            Assert.AreEqual(remoteDesktopAttributes.RemoteControlSettings, RemoteControlSettings.EnableNoInputNoNotify);
            Assert.AreEqual(remoteDesktopAttributes.RemoteDesktopHomeDirectory, "\\\\BOGUSSRV1\\hchick$");
            Assert.AreEqual(remoteDesktopAttributes.RemoteDesktopHomeDrive, "Y:");
            Assert.AreEqual(remoteDesktopAttributes.RemoteDesktopInitialProgram, "C:\\Windows\\Notepad.exe");
            Assert.AreEqual(remoteDesktopAttributes.RemoteDesktopProfilePath, "%systemroot%\\profiles\\custom\\default");
            Assert.AreEqual(remoteDesktopAttributes.RemoteDesktopWorkDirectory, "C:\\TEMP");

            userParameters = File.ReadAllText(@"..\..\..\Collateral\userParameters2.txt");
            attributes = new Dictionary<string, object[]>(StringComparer.OrdinalIgnoreCase);
            attributes.Add("userParameters", new string[] { userParameters });
            remoteDesktopAttributes = new RemoteDesktopAttributes();
            remoteDesktopAttributes.ProcessResults(attributes);

            Assert.IsTrue(remoteDesktopAttributes.IsValid);
            Assert.IsTrue(remoteDesktopAttributes.AllowLogon);
            Assert.IsFalse(remoteDesktopAttributes.ConnectClientDrivesAtLogon);
            Assert.IsFalse(remoteDesktopAttributes.ConnectClientPrintersAtLogon);
            Assert.IsFalse(remoteDesktopAttributes.DefaultToMainPrinter);
            Assert.AreEqual(remoteDesktopAttributes.DisconnectAction, RemoteDesktopDisconnectAction.Disconnect);
            Assert.AreEqual(remoteDesktopAttributes.MaxConnectionTime, TimeSpan.FromDays(2));
            Assert.AreEqual(remoteDesktopAttributes.MaxDisconnectionTime, TimeSpan.FromHours(3));
            Assert.AreEqual(remoteDesktopAttributes.MaxIdleTime, TimeSpan.FromDays(1));
            Assert.AreEqual(remoteDesktopAttributes.ReconnectAction, RemoteDesktopReconnectAction.FromAnyClient);
            Assert.IsFalse(remoteDesktopAttributes.RemoteControlEnabled);
            Assert.AreEqual(remoteDesktopAttributes.RemoteControlSettings, RemoteControlSettings.Disable);
            Assert.AreEqual(remoteDesktopAttributes.RemoteDesktopHomeDirectory, "\\\\BOGUSSRV1\\hchick$");
            Assert.AreEqual(remoteDesktopAttributes.RemoteDesktopHomeDrive, "Y:");
            Assert.AreEqual(remoteDesktopAttributes.RemoteDesktopInitialProgram, "C:\\Windows\\Notepad.exe");
            Assert.AreEqual(remoteDesktopAttributes.RemoteDesktopProfilePath, "%systemroot%\\profiles\\custom\\default");
            Assert.AreEqual(remoteDesktopAttributes.RemoteDesktopWorkDirectory, "C:\\TEMP");

        }

        [TestMethod]
        public void Test_RemoteDesktopAttributes_ProcessResults_DefaultSettings() {
            var userParameters = File.ReadAllText(@"..\..\..\Collateral\userParameters-DefaultValues.txt");
            var attributes = new Dictionary<string, object[]>(StringComparer.OrdinalIgnoreCase);
            attributes.Add("userParameters", new string[] { userParameters });
            var remoteDesktopAttributes = new RemoteDesktopAttributes();
            remoteDesktopAttributes.ProcessResults(attributes);

            Assert.IsTrue(remoteDesktopAttributes.IsValid);
            Assert.IsTrue(remoteDesktopAttributes.IsDefaultSettings);
        }

        [TestMethod]
        public void Test_ValidateComputerSamAccountName() {

            Debug.WriteLine(string.Format("BuiltinGuestsSid: {0}", new SecurityIdentifier(WellKnownSidType.BuiltinGuestsSid, null).Value));

            Debug.WriteLine("{0}", DateTime.Now.ToString());
            string samAccountName = "ABCDEFGHIJKLMN$";
            Assert.IsTrue(ADDataAccess.ComputerAccountMethods.IsComputerSamAccountNameValid(samAccountName));

            // Invalid characters: \/[]:;|=,+*?<>"
        }

        [TestMethod]
        public void Test_ValidateGroupSamAccountName() {

            Debug.WriteLine("{0}", DateTime.Now.ToString());
            string samAccountName = "abcdefghijklmnopqrstuvwxyz-01234567890-`~!@#$%^&()_{}'";
            Assert.IsTrue(ADDataAccess.GroupMethods.IsGroupSamAccountNameValid(samAccountName));

            samAccountName = " abcdefghijklmnopqrstuvwxyz-01234567890-`~!@#$%^&()_{}'";
            Assert.IsFalse(ADDataAccess.GroupMethods.IsGroupSamAccountNameValid(samAccountName));

            samAccountName = ".abcdefghijklmnopqrstuvwxyz-01234567890-`~!@#$%^&()_{}'";
            Assert.IsFalse(ADDataAccess.GroupMethods.IsGroupSamAccountNameValid(samAccountName));

            samAccountName = "abcdefghijklmnopqrstuvwxyz-01234567890-`~!@#$%^&()_{}' ";
            Assert.IsFalse(ADDataAccess.GroupMethods.IsGroupSamAccountNameValid(samAccountName));

            samAccountName = "abcdefghijklmnopqrstuvwxyz-01234567890-`~!@#$%^&()_{}'.";
            Assert.IsFalse(ADDataAccess.GroupMethods.IsGroupSamAccountNameValid(samAccountName));

            samAccountName = "01234567890012345678900123456789001234567890012345678900123456789012345";
            Assert.IsFalse(ADDataAccess.GroupMethods.IsGroupSamAccountNameValid(samAccountName));

            samAccountName = "fark-01234567890\\";
            Assert.IsFalse(ADDataAccess.GroupMethods.IsGroupSamAccountNameValid(samAccountName));

            samAccountName = "fark-01234567890/";
            Assert.IsFalse(ADDataAccess.GroupMethods.IsGroupSamAccountNameValid(samAccountName));

            samAccountName = "fark-01234567890[";
            Assert.IsFalse(ADDataAccess.GroupMethods.IsGroupSamAccountNameValid(samAccountName));

            samAccountName = "fark-01234567890]";
            Assert.IsFalse(ADDataAccess.GroupMethods.IsGroupSamAccountNameValid(samAccountName));

            samAccountName = "fark-01234567890:";
            Assert.IsFalse(ADDataAccess.GroupMethods.IsGroupSamAccountNameValid(samAccountName));

            samAccountName = "fark-01234567890;";
            Assert.IsFalse(ADDataAccess.GroupMethods.IsGroupSamAccountNameValid(samAccountName));

            samAccountName = "fark-01234567890|";
            Assert.IsFalse(ADDataAccess.GroupMethods.IsGroupSamAccountNameValid(samAccountName));

            samAccountName = "fark-01234567890=";
            Assert.IsFalse(ADDataAccess.GroupMethods.IsGroupSamAccountNameValid(samAccountName));

            samAccountName = "fark-01234567890,";
            Assert.IsFalse(ADDataAccess.GroupMethods.IsGroupSamAccountNameValid(samAccountName));

            samAccountName = "fark-01234567890+";
            Assert.IsFalse(ADDataAccess.GroupMethods.IsGroupSamAccountNameValid(samAccountName));

            samAccountName = "fark-01234567890*";
            Assert.IsFalse(ADDataAccess.GroupMethods.IsGroupSamAccountNameValid(samAccountName));

            samAccountName = "fark-01234567890?";
            Assert.IsFalse(ADDataAccess.GroupMethods.IsGroupSamAccountNameValid(samAccountName));

            samAccountName = "fark-01234567890<";
            Assert.IsFalse(ADDataAccess.GroupMethods.IsGroupSamAccountNameValid(samAccountName));

            samAccountName = "fark-01234567890>";
            Assert.IsFalse(ADDataAccess.GroupMethods.IsGroupSamAccountNameValid(samAccountName));

            samAccountName = "fark-01234567890\"";
            Assert.IsFalse(ADDataAccess.GroupMethods.IsGroupSamAccountNameValid(samAccountName));
        }

        [TestMethod]
        public void Test_UserCreationWhenNotInitialized() {
            var searchResults = new List<SearchResult>();

            using (var forestRootDirectoryEntry = new DirectoryEntry("GC://DC=contoso,DC=com"))
            using (var directorySearcher = new DirectorySearcher(forestRootDirectoryEntry)) {
                directorySearcher.SearchScope = System.DirectoryServices.SearchScope.Subtree;
                directorySearcher.Filter = "(&(objectCategory=person)(objectClass=user))";
                //directorySearcher.PropertiesToLoad.AddRange();
                directorySearcher.Tombstone = false;
                directorySearcher.ReferralChasing = ReferralChasingOption.All;
                directorySearcher.CacheResults = false;
                directorySearcher.PageSize = 1000;
                using (var results = directorySearcher.FindAll()) {
                    foreach (SearchResult result in results) {
                        searchResults.Add(result);
                    }
                }
            }

            foreach (var result in searchResults) {
                var account = new UserAccount(result, destroyInputObject: false);
                if (account.IsValid) {
                    Debug.WriteLine(string.Format("UserAccount DN: {0} Mail :{1}",
                         account.DN ?? "NULL", account.Mail ?? "NULL"));

                }
                Assert.IsTrue(account.IsValid);
            }

        }

        [TestMethod]
        public void Test_GetServerFromLdapDN() {
            var ldapDN = "GC://CONTOSOMDDC1.CONTOSO.COM/DC=contoso,DC=com";
            var serverFromLdapDN = ADDataAccess.GetServerFromLdapDN(ldapDN);
            Assert.AreEqual("CONTOSOMDDC1.CONTOSO.COM", serverFromLdapDN);
        }

        [TestMethod]
        public void Test_GenerateRandomPassword() {
            var stopwatch = Stopwatch.StartNew();
            var passwordCollection = new Dictionary<string, int>();

            for (int index = 0; index < 10000; index++) {
                var randomPassword= RandomPasswordGenerator.GeneratePassword().ReadString();
                Debug.WriteLine(randomPassword);
                Assert.AreEqual(24, randomPassword.Length);
                passwordCollection.Add(randomPassword, 0);
            }

            Debug.WriteLine(string.Format("Time required to generate 10000 passwords: {0}", stopwatch.Elapsed));
        }

    }
}
